from fastapi import Depends, FastAPI

import uvicorn
from starlette.responses import HTMLResponse
from starlette.responses import RedirectResponse
from starlette.staticfiles import StaticFiles

html_login='''
<form action="station/" method="get">
<h1>请登录</h1>
  <p>输入机台号码: <input type="text" name="q" autofocus="autofocus"></p>
  <input type="submit" value="Submit" />
</form>
'''
job='''
<head>

</head>
<body>
<form action="form_action.asp" method="get" onsubmit="retrun false">
<h2>{} {} {}</h2>

<p>机台号码: <input type="text" name="q" readonly="readonly" value={}></p>
<p>上一条记录: <input type="text" name="code0" readonly="readonly" value={}></p>
<p>本次期待值: <input type="text" name="codex" readonly="readonly" value={}></p>
  <p>First name: <input type="text" name="code1" autofocus="autofocus" ></p>
  <p>Last name: <input type="text" name="code2" required lay-verify="required" autocomplete="off" /></p>
  <input type="submit" value="Submit" />
</form>
<img src="/static/images/{}"  alt="上海鲜花港 - 郁金香" />
<body>
'''
app = FastAPI()
location='static'

app.mount('/static', StaticFiles(directory=location), name='static') 



@app.get("/")

def default():
    return HTMLResponse(content=html_login)
@app.get("/station/")
async def read_item1(q: str = None):
    if len(q)==4:
        return HTMLResponse(content=job.format('','','',q,0,0,'y1.jpg'))
    else:
        return RedirectResponse(url='/')

@app.get("/station/{item_id}")
async def read_item(item_id: str = None, q: str = None,code1: int = None,code2: int = None,codex: int = None,code0: int = None):
    print(code1,code2)
    if code1==0:
        return RedirectResponse(url='/')
        
    if code1==code2:
        if codex ==0:
        
            return HTMLResponse(content=job.format(code1,code2,'一致',q,code1,code1+1,'y1.jpg'))
        elif code1==codex:
            return HTMLResponse(content=job.format(code1,code2,'一致',q,code1,code1+1,'y1.jpg'))
        else:
            return HTMLResponse(content=job.format(code0,code0,'顺序不对',q,code0,codex,'l1.jpg'))
    else:
        return HTMLResponse(content=job.format(code0,code0,'不同',q,code0,codex,'w1.jpg'))

@app.get("/items/{item_id}")
async def read_item(item_id: str = None, q: str = None):
    return {"item_id": item_id, "q": q}



if __name__ == "__main__":   

    uvicorn.run(app, host="0.0.0.0",port=8000,log_level="info")
